package com.zky.seckillzky.seckill.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zky.common.utils.R;
import com.zky.seckillzky.seckill.service.UserService;
import com.zky.seckillzky.seckill.util.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;



/**
 * 这是用户表
 *
 * @author zky
 * @email zhongkaiyue@gmail.com
 * @date 2021-07-02 11:18:02
 */
@Controller
@RequestMapping("seckill/user")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }


    /**
     * @author zky
     * 登录
     */
    @RequestMapping("/dologin")
    @ResponseBody
    public R dologin(@RequestParam("mobile") String mobile, @RequestParam("password") String password, HttpServletResponse response){

        //判断手机号是否为空，判断手机号是否格式正确
        if(StringUtils.isEmpty(mobile)) {
            return R.ok().put("error","手机号为空");
        }
        if(!ValidatorUtil.isMobile(mobile)) {
            return R.ok().put("error","验证失败");
        }
        //判断密码是否为空
        if(StringUtils.isEmpty(password)) {
            return R.ok().put("error","手机号或者密码不正确");
        }

        //登录获取登录成功信息，暂时不返回登录对象数据，先返回正确的结果
        R token = userService.dologin(mobile, password, response);
        return token ;
    }




}
